﻿using System;
using System.Collections.Generic;

namespace ApproveFlowEngine.Split
{
    [AttributeUsage(AttributeTargets.Property)]
    public class SplitTableAttribute : Attribute
    {
        private readonly SplitTableType _splitTableType;

        public SplitTableAttribute(SplitTableType splitTableType)
        {
            _splitTableType = splitTableType;
        }
        /// <summary>
        /// 获取表后缀
        /// </summary>
        /// <param name="dateTime"></param>
        /// <returns></returns>
        public string GetTail(DateTime dateTime)
        {
            var format = string.Empty;
            switch (_splitTableType)
            {
                case SplitTableType.Year:
                    format = "yyyy";
                    break;
                case SplitTableType.Month:
                    format = "yyyyMM";
                    break;
                case SplitTableType.Day:
                    format = "yyyyMMdd";
                    break;
                default:
                    break;
            }
            return dateTime.ToString(format);
        }
        /// <summary>
        /// 检查表后缀是否有效
        /// </summary>
        /// <param name="tail"></param>
        /// <returns></returns>
        public bool Check(string tail)
        {
            switch (_splitTableType)
            {
                case SplitTableType.Year:
                    return DateTime.TryParse($"{tail}/01/01 00:00:00", out _);
                case SplitTableType.Month:
                    return DateTime.TryParse($"{tail[0..4]}/{tail[4..]}/01 00:00:00", out _);
                case SplitTableType.Day:
                    return DateTime.TryParse($"{tail[0..4]}/{tail[4..6]}/{tail[6..]} 00:00:00", out _);
                default:
                    break;
            }
            return false;
        }

        /// <summary>
        /// 检查表后缀是否符合时间范围
        /// </summary>
        /// <param name="tail"></param>
        /// <param name="begin"></param>
        /// <param name="end"></param>
        /// <returns></returns>
        public bool ValidateTail(string tail, DateTime? begin = null, DateTime? end = null)
        {
            var check = Check(tail);
            if (!int.TryParse(tail, out var tailInt))
            {
                return false;
            }
            if (!check)
            {
                return false;
            }

            if (begin is not null)
            {
                if (int.TryParse(GetTail(begin.Value), out var beginInt))
                {
                    if (tailInt < beginInt)
                    {
                        return false;
                    }
                }
            }
            if (end is not null)
            {
                if (int.TryParse(GetTail(end.Value), out var endInt))
                {
                    if (tailInt > endInt)
                    {
                        return false;
                    }
                }
            }
            return true;
        }
        /// <summary>
        /// 根据时间过滤无效表
        /// </summary>
        /// <param name="begin"></param>
        /// <param name="end"></param>
        /// <param name="tails"></param>
        /// <returns></returns>
        public List<string> FilterTail(DateTime? begin, DateTime? end, List<string> tails)
        {
            var result = new List<string>();
            foreach (var tail in tails)
            {
                if (ValidateTail(tail, begin, end))
                {
                    result.Add(tail);
                }
            }
            return result;
        }
    }
}
